package com.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * 百度AI人脸识别组件
 * @author Xiao
 * @date 2021/10/22
 */
public class AipFaceTemplate {
    @Autowired
    private AipFace client;

    public boolean detect(String imageUrl) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        String image = imageUrl;
        String imageType = "URL";

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
//        System.out.println(res.toString(2));

        Integer err_code = (Integer) res.get("error_code");
//        System.out.println(err_code);
        return err_code == 0;
    }
}
